<?php 
class IndexController{
	private $data;
	public function __construct(){
	   $this->data = include './data.php';
	}
	public function index(){
		$data = $this->data;
		include './view/head.html';
	}
	public function add(){
		if(IS_POST){
			$_POST['sendtime'] = time();
			$this->data[] = $_POST;
			file_put_contents('./data.php', "<?php return ". var_export($this->data,true) . " ?>");
			success('添加成功','Index.php');
		}
		include 'view/add.html';
	}
	public function del(){
		$data = include './data.php';
		$id = intval($_GET['id']);
		unset($data[$id]);
		file_put_contents('./data.php', "<?php\r\nreturn " . var_export($data , true) . "\r\n ?>");
		success('删除成功', './Index.php');
	}
	public function edit(){
		$id = intval($_GET['id']);
		
		$bool = $this->check($id);
		if(!$bool) success('文章不存在', 'Index.php');
		
		$data = $this->data;
		$oldData = $data[$id];
		
		if(IS_POST){
			$_POST['sendtime'] = time();
			$this->data[] = $_POST;
			$data[$id] = $_POST;
			file_put_contents('./data.php',"<?php return ". var_export($data,true)."?>");
			success('修改成功', 'Index.php');
		}
		include 'view/edit.html';
	}
	public function show(){
		$id = intval($_GET['id']);
		if(!$this->check($id)){
			success('该文章不存在', 'Index.php');
		}else{
			$oldData =  $this->data[$id];
			include 'view/show.html';
		}
		
	}
	//检测文章是否存在
	private function check($id){
		if(!isset($this->data[$id])){
			return FALSE;
		}
		return true;
	}
}











 ?>